package com.qf.controller;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/sendMsg")
    public String sendMsg(String msg) {
        System.out.println("msg:" + msg);
        rabbitTemplate.convertAndSend("order-exchange", "order.insert", msg);
        return "【" + msg + "】发送成功";
    }

    @RequestMapping("/sendMsgTtl")
    public String sendMsgTtl(String msg) {
        String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        System.out.println("发送的时间是:"+now+",内容:"+msg);
        if ("5".equals(msg)) {
            // 5s的超时队列中
            rabbitTemplate.convertAndSend("order-exchange", "ttl.5", "ttl-" + msg);
        } else if ("10".equals(msg)) {
            // 10s的超时队列中
            rabbitTemplate.convertAndSend("order-exchange", "ttl.10", "ttl-" + msg);
        }
        return "ok";
    }
}
